/**
 * 格式化时间戳与当前时间差
 * 超过 3 个月则显示 “3个月前”
 * @param {number|string} timestamp - 秒或毫秒时间戳
 * @returns {string}
 */
export function formatTimestamp(timestamp) {
    if (!timestamp) return '';

    timestamp = Number(timestamp);
    if (timestamp < 1e12) {
        // 秒转毫秒
        timestamp *= 1000;
    }

    const now = Date.now();
    const diff = now - timestamp;

    if (diff < 0) {
        return '刚刚';
    }

    const seconds = Math.floor(diff / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    const days = Math.floor(hours / 24);
    const months = Math.floor(days / 30);

    if (seconds < 60) {
        return '刚刚';
    } else if (minutes < 60) {
        return `${minutes}分钟前`;
    } else if (hours < 24) {
        return `${hours}小时前`;
    } else if (days < 30) {
        return `${days}天前`;
    } else if (months < 3) {
        return `${months}个月前`;
    } else {
        return '3个月前';
    }
}

export const goHome = () => {
    uni.showModal({
        title: '提示',
        content: '加载失败,即将返回首页',
        showCancel: false,
        success: ({ confirm, cancel }) => {
            if (confirm) {
                uni.reLaunch({
                    url: '/pages/index/index'
                })
            }
        }
    })
}
